<template>
  <a-table
    ref="table"
    size="middle"
    bordered
    rowKey="id"
    :columns="columns"
    :dataSource="dataSource"
    :pagination="ipagination"
    :loading="loading"
    :rowSelection="{selectedRowKeys: computedKeys, onChange: onSelectChange}"
    @change="handleTableChange">
  </a-table>
</template>

<script>

  import { YcListMixin } from '@/mixins/YcListMixin'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'SelectPositionList',
    props: {
      value: {
        type: Array,
        required: true
      }
    },
    mixins: [YcListMixin],
    data () {
      return {
        columns: [
          {
            title: this.$t('m.system.position.entity.positionName'),
            align: 'center',
            dataIndex: 'name'
          },
          {
            title: this.$t('m.system.position.entity.positionCode'),
            align: 'center',
            dataIndex: 'positionCode'
          },
        ],
        url: {
          list: YchConstant.PREFIX_BPM + '/bpm-back/processSelectRest/getPositionListByPage/v1',
        },
      }
    },
    computed: {
      computedKeys () {
        return this.value.map(item => item.id)
      }
    },
    methods: {
      onSelectChange (selectedRowKeys, selectedRows) {
        const oldKeys = this.computedKeys
        const data = this.value.filter(item => selectedRowKeys.indexOf(item.id) != -1)//原来剩下的
        const addIds = selectedRowKeys.filter(id => oldKeys.indexOf(id) == -1)//新增的
        selectedRows.forEach(item => {
          if (addIds.indexOf(item.id) != -1) {
            data.push({
              id: item.id,
              name: item.name
            })
          }
        })
        this.$emit('input', data)
      }
    }
  }
</script>
